<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="门店名称">
              <a-input v-model="queryParam.name"  placeholder="请输入"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="区域">
              <a-cascader v-model="queryParam.areas" placeholder="省市区" :options="options" changeOnSelect/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="上级机构">
              <a-tree-select
                :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
                placeholder='请选择'
                allowClear
                treeDefaultExpandAll
                :treeData="treeData"
                v-model="queryParam.code"
              >
              <span style="color:#08c" slot="title" slot-scope="{key}"
                    v-decorator="['key',{rules: [{ required: true, message: '请选择门店' }]}]">
                {{value}}
              </span>>
              </a-tree-select>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button v-action:query type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button v-action:query style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <s-table
      ref="table"
      size="default"
      :columns="columns"
      :data="loadData"
    >
      <span slot="state" slot-scope="text">
        {{ text | statusFilter }}
      </span>

      <span slot="urlList" slot-scope="text,record" class="storUrl">
          <template>
            <div  class="urlitem"  v-for="(item,index) in record.urlList">
              <img class="itemImg" v-image-preview :src="item" alt="" style="width: 50px;height: auto;">
            </div>
        </template>
      </span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a v-action:update @click="$refs.modal.edit(record)">分配管理</a>
        </template>
      </span>
    </s-table>


    <Edit ref="modal" @ok="handleOk"></Edit>
  </a-card>
</template>

<script>

  import STable from '@/components/table/'
  import Edit from './Edit'
  import { selectList, del, selectAllArea,selectTree } from '@/api/storeService/store'
  import { selectAdvertisementList} from '@/api/storeService/store'

  export default {
    components: {
      STable,
      Edit
    },
    //--############  data() ###############-->
    data() {
      return {
        description: '',
        // 高级搜索 展开/关闭
        advanced: false,
        // 查询参数
        queryParam: {},
        options: [],
        // 表头
        columns: [
          {
            title: '商户',
            width: '20%',
            dataIndex: 'merchantName'
          },
          {
            title: '门店名称',
            width: '20%',
            dataIndex: 'name'
          },
          {
            title: '广告图片',
            dataIndex: 'urlList',
            scopedSlots: { customRender: 'urlList' }
          },

          {
            title: '操作',
            dataIndex: 'action',
            width: '15%',
            scopedSlots: { customRender: 'action' }
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          let areas = this.queryParam.areas
          if (areas != null || areas != undefined ) {
            let areaParam = ''
            for (let i = 0; i < areas.length; i++) {
              if (i == areas.length - 1) {
                areaParam = areaParam + areas[i]
              } else {
                areaParam = areaParam + areas[i] + ','
              }
            }
            this.queryParam.area = areaParam
          }

          return selectAdvertisementList(Object.assign(parameter, this.queryParam))
            .then(res => {
              return res.result

            })

        },
        treeData: [],
        selectedRowKeys: [],
        selectedRows: []
      }
    },

    created() {
      const _this = this
      const params = { 'ctrlType': '1,2', 'displayType': '1,2,3' }
      /*ctrlType:控制层级结构不能操作，1，服务商，2，代理商，3商户，4，门店
      * displayType:控制层级显示，必须是连续，1，服务商，2，代理商，3商户，4，门店
      * */

      selectTree(params).then(res => {
        if (res.code === 200) {
          if (res.result == null) {
            return
          }
          _this.treeData = res.result
        } else {
          this.$notification.error({
            message: '错误',
            description: res.message,
            duration: 4
          })
        }
      })

      selectAllArea().then(res => {
          this.options = res.result
        }
      )
    //  ---------


    },
    methods: {
      handleDel(row) {
        const _this = this
        this.$confirm({
          title: '警告',
          content: `真的要删除 ${row.name} 吗?`,
          okText: '删除',
          okType: 'danger',
          cancelText: '取消',
          onOk() {
            // 在这里调用删除接口
            del(row.id).then(res => {
              if (res.code === 200) {
                _this.$message.success('删除成功')
                _this.$emit('ok')
                _this.$refs.table.refresh()
              }
              else {
                _this.$notification.error({
                  message: '错误',
                  description: res.message,
                  duration: 4
                })
              }
              // _this.$message.info(res.message)

            })
          },
          onCancel() {
            console.log('Cancel')
          }
        })
      },
      handleOk() {
        // 新增/修改 成功时，重载列表
        this.$refs.table.refresh(false)
      },

      onSelectChange(selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      toggleAdvanced() {
        this.advanced = !this.advanced
      }
    },
    //--############  methods() ###############-->
    //--############  filters() ###############-->
    filters: {
      statusFilter(state) {
        const statusMap = {
          false: '未使用',
          true: '已使用'
        }
        return statusMap[state]
      }
    }
    //--############  filters() ###############-->
  }
</script>
<style  scoped>
  .storUrl{
    display: flex;
    align-items: center;
    align-items: center;
  }
  .urlitem{
    height: 80px;
    width: 50px;
    margin-left: 20px;
    overflow: hidden;
  }
  .itemImg{
    width: 100%;
    height: 100%;
  }
</style>
